package com.example.demo;


import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/User")
@RestController
public class UserController {

    @PostMapping("/login")
    public Boolean login(String userName , String password , HttpSession session) {
        //参数校验
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }

        //参数比对
        if("zhangsan".equals(userName) && "123456".equals(password)) {
            //存储session
            session.setAttribute("loginUser",userName);
            return true;
        }
        return false;
    }

    @GetMapping("/getLoginUser")
    public String getLoginUser(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session != null) {
            String loginUser = (String)session.getAttribute("loginUser");
            return loginUser;
        }
        return "";
    }
}
